#include "server.hpp"

class Echoserver {
private:
Tcpserver _tcp_server;//TCP服务器

void Onconnected(const PtrConnection& conn)
{
    cout<<"in Onconnected"<<endl;
    DBG_LOG("connected: %d", conn->Fd());
    
}

void OnMessage(const PtrConnection& conn, Buffer* buf)//处理接收到的消息
{
    DBG_LOG("message: %d", conn->Fd());
   char buff[1024]={0};
   buf->ReadandPop(buff, buf->Readablesize());//从输入缓冲区读取数据到buff中
   DBG_LOG("received data: %s", buff);
  
   std::string str="hello world";
   conn->Send(str.c_str(), str.size());//发送字符串到客户端
}

void connectionDestroy(const PtrConnection& conn)
{
    DBG_LOG("connection destroy: %d", conn->Fd());
    // 连接销毁时的处理逻辑
}
public:

Echoserver(int port):_tcp_server(port)
{
   _tcp_server.SetThreadCount(2);//设置线程池线程个数为2
   _tcp_server.EnableInactiveRelease(10);//允许非活跃连接释放，超时时间为5秒
   _tcp_server.SetCloseCallback(std::bind(&Echoserver::connectionDestroy,this,std::placeholders::_1));//设置连接销毁回调函数
   _tcp_server.SetConnectedCallback(std::bind(&Echoserver::Onconnected,this,std::placeholders::_1));//设置连接回调函数
   _tcp_server.SetMessageCallback(std::bind(&Echoserver::OnMessage,this,std::placeholders::_1,std::placeholders::_2));//设置消息回调函数
}

void Start()
{
    _tcp_server.Start();//启动TCP服务器
}
};